<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2018/4/18
 * Time: 9:48
 */

namespace app\snpdetection\controller;


use app\common\controller\BaseController;

class Disease extends BaseController
{
    protected $model = 'app\snpdetection\model\Disease';

    public function getTemplatePath()
    {
        return $_SERVER['DOCUMENT_ROOT'] . DS . 'template' . DS . 'snpdetection';
    }

    public function getTemplatePathDefault()
    {
        return $this->getTemplatePath() . DS . 'default';
    }

    public function getReportPath()
    {
        return $_SERVER['DOCUMENT_ROOT'] . DS . 'report' . DS . 'snpdetection';
    }

    /**
     * @return mixed
     * 上传模板
     */
    public function uploadTemplate()
    {
        try {
            $diseaseId = input()['diseaseId'];
            if (!$this->M->get($diseaseId)) throw new \Exception('错误的疾病ID：' . $diseaseId);
            $file = request()->file('file');
            if ($file) {
                //保存文件
                $savePath = $this->getTemplatePath();
                $file->move($savePath, $diseaseId . '.docx');
                $defaultFile = $this->getTemplatePathDefault() . DS . $diseaseId . '.docx';
                if (!file_exists($defaultFile)) {
                    copy($savePath . DS . $diseaseId . '.docx', $defaultFile);
                }
                return true;
            } else {
                return false;
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param string $diseaseName
     * 下载模板
     */
    public function downloadTemplate($diseaseName)
    {
        if (file_exists($this->getTemplatePathDefault() . DS . iconv('UTF-8', 'GB2312//IGNORE',$diseaseName) . '.docx')) {
            $this->redirect('/template/snpdetection/default/' . iconv('UTF-8', 'GB2312//IGNORE',$diseaseName) . '.docx');
        } else {
            exit('文件不存在');
        }

    }


}